<template>
  <a-modal
      title="新建站点"
      :width="320"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleSubmit"
      @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
            label="站点名称"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['stationName', {rules: [{required: true, message: '请输入站点名称'}]}]"/>
        </a-form-item>
        <a-form-item
            label="站点地址"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['stationPosition', {rules: [{required: true, message: '请输入站点地址'}]}]"/>
        </a-form-item>
        <a-form-item
            label="备注信息"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['stationInfo']"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import {addBike} from '@/api/system/bike'

export default {
  data () {
    return {
      labelCol: {
        xs: {span: 24},
        sm: {span: 7}
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16}
      },
      visible: false,
      confirmLoading: false,

      form: this.$form.createForm(this)
    }
  },
  methods: {
    add () {
      this.visible = true
    },
    handleSubmit () {
      const {form: {validateFields}} = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          addBike(values)
              .then(res => {
                if (res.message !== 'error') {
                  this.visible = false
                  this.confirmLoading = false
                  this.form.resetFields()
                  this.$emit('ok')
                } else {
                  this.confirmLoading = false
                  this.$message.error('操作失败！')
                }
              })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
